package com.dubbo.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 作者: LCG
 * 日期: 2019/9/11 20:12
 * 描述:
 */
public class DubboServer {

    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {

        ServerSocket serverSocket=new ServerSocket();

        serverSocket.bind(new InetSocketAddress("127.0.0.1",10000));

        while (true){

            Socket accept = serverSocket.accept();

            ObjectInputStream ois=new ObjectInputStream(accept.getInputStream());

            Object[] pargs=(Object[]) ois.readObject();//传递的参数

            String className=(String)ois.readUTF();////实现类的名称

            String methodName=(String)ois.readUTF();////方法的名称

            Class<?> [] paramterTypes=(Class<?> [])ois.readObject();//参数类型

            Class<?> service=Class.forName(className);//实现类的实体

            System.out.println("------1-----"+pargs);
            System.out.println("------2-----"+className);
            System.out.println("------3-----"+methodName);
            System.out.println("------4-----"+paramterTypes);

            Method method=service.getMethod(methodName, paramterTypes);//获得执行的方法

            Object result=method.invoke(service.newInstance(),pargs);


            //写回数据
            ObjectOutputStream oos=new ObjectOutputStream(accept.getOutputStream());

            oos.writeObject(result);


        }

    }

}
